package com.topone.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(WxPayService.class)
public class WechatPayConfiguration {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private WechatPayProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public WxPayConfig wxPayConfig() throws WxPayException {
        WxPayConfig wxPayConfig=new WxPayConfig();
        wxPayConfig.setAppId(this.properties.getAppId());
        wxPayConfig.setMchId(this.properties.getMchId());
        wxPayConfig.setMchKey(this.properties.getMchKey());
        wxPayConfig.setNotifyUrl(this.properties.getNotifyUrl());
        wxPayConfig.setTradeType("JSAPI");
        wxPayConfig.setKeyPath(this.properties.getKeyPath());
        wxPayConfig.setSignType("MD5");
        wxPayConfig.setSubMchId("1514046441");
        wxPayConfig.setSubAppId("wx7c48a2b20c836127");
        //wxPayConfig.setSslContext(wxPayConfig.initSSLContext());
        return wxPayConfig;
    }


    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxPayService(WxPayConfig wxPayConfig){
        WxPayService wxPayService=new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }
}
